#!/usr/bin/perl -w

	use LWP 5.64;
	use HTTP::Cookies; 
	use Fcntl;
	use MIME::Base64 qw(encode_base64);
	use Digest::MD5  qw(md5_hex);
	
	my $use_proxy = 'http://127.0.0.1:8118/'; # proxy address here
	
	my $login_mail = 'mail';
	my $login_pass = 'pass';
	my $userid = 0;
	my $groupid = 1495173;
	my $useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1;) Firefox/2.0.0.0';
	
#	my $img_name = 'blackoverlord.png';
	my $img_dir = "./nigra"; # dir with images. put nigras here
	
	my $url_login = 'http://vkontakte.ru/login.php';
	my $url_friends = 'http://vkontakte.ru/friend.php';
	my $url_send = "http://vkontakte.ru/graffiti.php?to_id=$userid&group_id=$groupid";
	my $url_post = 'http://vkontakte.ru/wall.php';
	my $url_redirect = 'http://vkontakte.ru/graffiti.php?act=last';
	
	my $ua = LWP::UserAgent->new(keep_alive => 1);
	$ua->proxy('http', $use_proxy) if $use_proxy;
	$ua->agent($useragent);
	$ua->cookie_jar({});
	
	print 'Login... ';
	my $response = $ua->post($url_login, 
		[	'email' => $login_mail, 
			'pass' => $login_pass, 
		]);
	die 'Login error -- ', $response->status_line
		unless $response->is_success or $response->code == 302;
	print "Success.\n";
	
	opendir(DIR,$img_dir) or die "opendir error!";
	@images= readdir(DIR);
	closedir(DIR);

	
LOOP:	foreach $img_name (@images) {
		next if(($img_name eq ".") || ($img_name eq ".."));
		my $buff;
		my $img_data;
		my $hash_data;
		print "IMAGE: $img_dir/$img_name\n";
		print 'Checksum... '; # calculate md5 checksum from first 1024 bytes of base64 encoded image
		open(IMG, "$img_dir/$img_name") or do { warn "open error!"; next LOOP; };
		binmode IMG;
		while (read(IMG, $buff, 60*57)) {
			$img_data .= $buff;
			$hash_data .= encode_base64($buff,'');
		}
		close IMG;

		my $hash = md5_hex substr $hash_data, 0, 1024;
		print "Success (MD5: $hash).\n"; 
		
		my $img_header = '----OLEG-ANDREEV-PAVEL-DUROV-GRAFFITI-POST'."\n" .
			'Content-Disposition: form-data; name="Signature"' . "\n"."\n" .
			"$hash" . "\n".
			'----OLEG-ANDREEV-PAVEL-DUROV-GRAFFITI-POST'      . "\n" .
			'Content-Disposition: form-data; name="Filedata"; filename="graffiti.png"' . "\n" .
			'Content-Type: image/png' ."\n"."\n";

		my @post_header = ('Referer' => 'http://vkontakte.ru/swf/Graffiti.swf?12',
			'Host' => 'vkontakte.ru',
			'Accept' => 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,/*;q=0.5',
			'Accept-Language' => 'ru,en-us;q=0.7,en;q=0.3',
			'Accept-Charset' => 'windows-1251,utf-8;q=0.7,*;q=0.7',
			'Keep-Alive' => '300',
			'Connection' => 'keep-alive',
			);
		print 'Send... ';
		$response = $ua->post(
			$url_send, @post_header,
			'Content_Type' => 'multipart/form-data; boundary=--OLEG-ANDREEV-PAVEL-DUROV-GRAFFITI-POST',
			'Content' => "$img_header"."$img_data"."\n----OLEG-ANDREEV-PAVEL-DUROV-GRAFFITI-POST\n",
		);
			
		do { warn "Sending data error -- ", $response->status_line; next LOOP; }
			unless $response->is_success or $response->code == 302;
		print 'Success. ';
		
		print 'Redirect... ';
		$response = $ua->get($url_redirect);
		do { warn "Sending data error -- ", $response->status_line; next; }
			unless $response->is_success;
		print 'Success. ';
		$_ = $response->content;

		my ($to_id) = /name=\"to_id\" value=\"(\w+)\"/;
		my ($grid) = /name=\"grid\" value=\"(\d+)\" /;
		my ($wall_hash) = /name=\"wall_hash\" value=\"(\w+)\" /;
		my ($message) = /name=\"message\" value=\"(\d+)\" /;
		
		print 'Post image... ';
		$response = $ua->post( $url_post,
			[ 'to_id' => $to_id, 'grid' => $grid, 'act' => 'sent', 'wall_hash' => $wall_hash, 'message' => $message, ]
		);
		do { warn "Posting image error: ", $response->status_line; next LOOP; }
			unless $response->is_success or $response->code == 302;
		print "Success.\n";
		undef $img_data;
		undef $hash_data;
#		sleep 1;
	}